//
//  LLOrderViewController.m
//  UserUse
//
//  Created by Yiwi on 16/10/18.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "LLOrderViewController.h"
#import "LLOrderView.h"
#import "LLOrderDetailViewController.h"
#import "LoginViewController.h"
#import "LLOrderListModel.h"
#import "HomeOrderModel.h"
#import "SendDetailViewController.h"

@interface LLOrderViewController ()<LLOrderViewDelegate>{
    LLOrderView * _orderView;
    NSMutableArray * _orderList;
    NSInteger _page;
}

@property(nonatomic,strong)NSMutableArray *orderLists;


@end

@implementation LLOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的派单";
    [self setupSubView];
//    [self reloadFristPageListData];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self reloadFristPageListData];
}
- (void)setupSubView{
    _orderView = [LLOrderView new];
    _orderView.delegate = self;
    _orderView.frame = kScreenFrame;
    [self.view addSubview:_orderView];
}
-(void)reloadData{
    
    LLUserModel * user = [LLUserHandle getUserData];
    
    if (user == nil ) {
        LoginViewController * loginVC = [LoginViewController new];
        [self.navigationController pushViewController:loginVC animated:YES];
        return;
    }
    [AllProjectRequest queryAllOrderByDriver:user.userid  success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NSArray *array = responseObject[@"resultObject"];
        _orderList = [HomeOrderModel arrayOfModelsFromDictionaries:array error:nil];
        _orderView.orderLists = [_orderList mutableCopy];
        [_orderView setupSubView];
        [self endRefresh];
    } failure:^(NSError *error) {
        [self endRefresh];
    }];
//    [AllProjectRequest checkOrderWithUserId:user.userid WithPage:_page success:^(id responseObject){
//        if (_page == 1) {
//            [_orderLists removeAllObjects];
//            [_orderView.orderLists removeAllObjects];
//        }
//        [self.orderLists addObjectsFromArray:responseObject];
//        [_orderView.orderLists addObjectsFromArray:responseObject];
//        [_orderView setupSubView];
//        _page++;
//        [self endRefresh];
//    } failure:^(NSString *error) {
//        [self endRefresh];
//    }];
}
- (void)endRefresh {
    if (_orderView.iTabelView.mj_header) {
        [_orderView.iTabelView.mj_header endRefreshing];
    }
    if (_orderView.iTabelView.mj_footer) {
        [_orderView.iTabelView.mj_footer endRefreshing];
    }
}
#pragma mark-LLOrderViewDelegate
-(void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SendDetailViewController *sendDetail = [[SendDetailViewController alloc]init];
    HomeOrderModel *model = _orderList[indexPath.row];
    sendDetail.orderId = model.order_id;
    sendDetail.start = model.route_s;
    sendDetail.end = model.route_e;
    [self.navigationController pushViewController:sendDetail animated:YES];
}

-(void)reloadFristPageListData{
    _page = 1;
    [self reloadData];
}
-(void)reloadNextPageListData{
    [self reloadData];
}
-(void)completeWithListModel:(LLOrderListModel *)listmodel;
{


}
#pragma mark - 懒加载
-(NSMutableArray *)orderLists{
    if (!_orderLists) {
        _orderLists  = [NSMutableArray new];
    }
    return _orderLists;
}

@end
